package domain;

// Generated 19.Eki.2012 14:01:16 by Hibernate Tools 3.4.0.CR1

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import static javax.persistence.GenerationType.SEQUENCE;

import javax.persistence.CascadeType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.OrderBy;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;

import exception.IBToolsException;

import util.GlobalUtils;
import util.HibernateUtil;

import manager.LogManager;

/**
 * LogServiceCallDAO generated by hbm2java
 */
@Entity
@Table(name = "IBTOOLS_LOG_SERVICE_CALL", schema = "T32556")
public class LogServiceCallDAO implements java.io.Serializable {
	
	/***
	 * Added : cascade = CascadeType.ALL
	 * @Temporal(TemporalType.TIMESTAMP)
	 */

	private Long oid;
	private String userCode;
	private String callDatetime;
	private String callerIp;
	private String targetIp;
	private String targetServer;
	private Integer environment;
	private boolean success;
	private String errorType;
	private String serviceName;
	private Date callDatetimestamp;
	private Set<LogServiceCallErrorDAO> logServiceCallErrorList = new HashSet<LogServiceCallErrorDAO>(
			0);
	private Set<LogServiceCallDataDAO> logServiceCallDataList = new HashSet<LogServiceCallDataDAO>(
			0);
	private Set<LogServiceCallOutDAO> logServiceCallOutList = new HashSet<LogServiceCallOutDAO>(
			0);

	public LogServiceCallDAO() {
	}

	public LogServiceCallDAO(String userCode, String callDatetime,
			String callerIp, String targetServer, boolean success,
			String serviceName) {
		this.userCode = userCode;
		this.callDatetime = callDatetime;
		this.callerIp = callerIp;
		this.targetServer = targetServer;
		this.success = success;
		this.serviceName = serviceName;
	}

	public LogServiceCallDAO(String userCode, String callDatetime,
			String callerIp, String targetIp, String targetServer,
			boolean success, String errorType, String serviceName,
			Date callDatetimestamp,
			Set<LogServiceCallErrorDAO> logServiceCallErrorList,
			Set<LogServiceCallDataDAO> logServiceCallDataList) {
		this.userCode = userCode;
		this.callDatetime = callDatetime;
		this.callerIp = callerIp;
		this.targetIp = targetIp;
		this.targetServer = targetServer;
		this.success = success;
		this.errorType = errorType;
		this.serviceName = serviceName;
		this.callDatetimestamp = callDatetimestamp;
		this.logServiceCallErrorList = logServiceCallErrorList;
		this.logServiceCallDataList = logServiceCallDataList;
	}

	@SequenceGenerator(name = "generator")
	@Id
	@GeneratedValue(strategy = SEQUENCE, generator = "generator")
	@Column(name = "OID", unique = true, nullable = false, scale = 0)
	public Long getOid() {
		return this.oid;
	}

	public void setOid(Long oid) {
		this.oid = oid;
	}

	@Column(name = "USER_CODE", nullable = false, length = 10)
	public String getUserCode() {
		return this.userCode;
	}

	public void setUserCode(String userCode) {
		this.userCode = userCode;
	}

	@Column(name = "CALL_DATETIME", nullable = false, length = 20)
	public String getCallDatetime() {
		return this.callDatetime;
	}

	public void setCallDatetime(String callDatetime) {
		this.callDatetime = callDatetime;
	}

	@Column(name = "CALLER_IP", nullable = false, length = 20)
	public String getCallerIp() {
		return this.callerIp;
	}

	public void setCallerIp(String callerIp) {
		this.callerIp = callerIp;
	}

	@Column(name = "TARGET_IP", length = 20)
	public String getTargetIp() {
		return this.targetIp;
	}

	public void setTargetIp(String targetIp) {
		this.targetIp = targetIp;
	}

	@Column(name = "TARGET_SERVER", nullable = false, length = 30)
	public String getTargetServer() {
		return this.targetServer;
	}

	public void setTargetServer(String targetServer) {
		this.targetServer = targetServer;
	}

	@Column(name = "SUCCESS", nullable = false, precision = 1, scale = 0)
	public boolean isSuccess() {
		return this.success;
	}

	public void setSuccess(boolean success) {
		this.success = success;
	}

	@Column(name = "ERROR_TYPE", length = 10)
	public String getErrorType() {
		return this.errorType;
	}

	public void setErrorType(String errorType) {
		this.errorType = errorType;
	}

	@Column(name = "SERVICE_NAME", nullable = false, length = 70)
	public String getServiceName() {
		return this.serviceName;
	}

	public void setServiceName(String serviceName) {
		this.serviceName = serviceName;
	}

	@Column(name = "ENVIRONMENT")
	public Integer getEnvironment() {
		return environment;
	}

	public void setEnvironment(Integer environment) {
		this.environment = environment;
	}
	
	@Temporal(TemporalType.TIMESTAMP)
	@Column(name = "CALL_DATETIMESTAMP", length = 7)
	public Date getCallDatetimestamp() {
		return this.callDatetimestamp;
	}

	public void setCallDatetimestamp(Date callDatetimestamp) {
		this.callDatetimestamp = callDatetimestamp;
	}

	@OneToMany(fetch = FetchType.LAZY, mappedBy = "logServiceCall", cascade = CascadeType.ALL)
	public Set<LogServiceCallErrorDAO> getLogServiceCallErrorList() {
		return this.logServiceCallErrorList;
	}

	public void setLogServiceCallErrorList(
			Set<LogServiceCallErrorDAO> logServiceCallErrorList) {
		this.logServiceCallErrorList = logServiceCallErrorList;
	}

	@OneToMany(fetch = FetchType.LAZY, mappedBy = "logServiceCall", cascade = CascadeType.ALL)
	public Set<LogServiceCallDataDAO> getLogServiceCallDataList() {
		return this.logServiceCallDataList;
	}

	public void setLogServiceCallDataList(
			Set<LogServiceCallDataDAO> logServiceCallDataList) {
		this.logServiceCallDataList = logServiceCallDataList;
	}

	@OneToMany(fetch = FetchType.LAZY, mappedBy = "logServiceCall", cascade = CascadeType.ALL)
	public Set<LogServiceCallOutDAO> getLogServiceCallOutList() {
		return logServiceCallOutList;
	}

	public void setLogServiceCallOutList(
			Set<LogServiceCallOutDAO> logServiceCallOutList) {
		this.logServiceCallOutList = logServiceCallOutList;
	}

	@Transient
	public String getErrorMessage() throws IBToolsException{
		
//		String message = "";
//		ArrayList<LogServiceCallErrorDAO> sortedErrors = new ArrayList<LogServiceCallErrorDAO>(logServiceCallErrorList);
//		Collections.sort(sortedErrors, new Comparator<LogServiceCallErrorDAO>(){
//		     public int compare(LogServiceCallErrorDAO o1, LogServiceCallErrorDAO o2){
//		         if(o1.getPart() == o2.getPart())
//		             return 0;
//		         return o1.getPart() < o2.getPart() ? -1 : 1;
//		     }
//		});
//		
//		int i = 0;
//		String test = "";
//		for (LogServiceCallErrorDAO callErrorDAO : logServiceCallErrorList) {
//			i++;
//			test+=callErrorDAO.getErrorText();
//		}
//		for (LogServiceCallErrorDAO callErrorDAO : sortedErrors) {
//			message += callErrorDAO.getErrorText();
//		}
//		return message;
//		return LogManager.loadServiceCallErrorLog(this);
		return "";
	}

	public void setErrorMessage(String dummy){
		
	}
	
	public void loadOutSampleMessage() throws IBToolsException{
		outSampleMessage = GlobalUtils.getSizeLimitedText(LogManager.loadServiceCallOutLog(this, true), 40);

		if(outSampleMessage.equalsIgnoreCase("")){
			outSampleMessage = "-";
			outputUnavailable = true;
		}
		else {
			outputUnavailable = false;
		}
		outSampleMessage = outSampleMessage.replaceAll("\n", "");
	}

	private boolean outputUnavailable;
	private String outSampleMessage;
	
	@Transient
	public boolean isOutputUnAvailable() throws IBToolsException{
		return outputUnavailable;
	}
	public void setOutputUnAvailable(boolean outputUnavailable) throws IBToolsException{
		
	}
	@Transient
	public String getOutSampleMessage() throws IBToolsException{
		return outSampleMessage;
	}
	
	public void setOutSampleMessage(String dummy){
		
	}
}
